package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.MeetingMapper;
import com.guoning.office_system.model.*;
import com.guoning.office_system.service.AccountingRecordService;
import com.guoning.office_system.service.MeetingService;
import com.guoning.office_system.service.UserService;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MeetingServiceImpl implements MeetingService {
    @Autowired
    MeetingMapper meetingMapper;
    @Autowired
    AccountingRecordService accountingRecordService;
    @Autowired
    UserService userService;

    @Override
    public Map getTabsCount(Criteria criteria) {
        Long meetingCount = meetingMapper.selectByUserCount(criteria);
        Long recordCount = accountingRecordService.selectByConditionCount(criteria);
        Map map = new HashMap();
        map.put("meeting", meetingCount);
        map.put("record", recordCount);
        return map;
    }

    @Override
    public PageBean selectByUser(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex()*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<MeetingWithRecordAndUser> list = meetingMapper.selectByUser(criteria);
        for (int i=0; i<list.size(); i++){
            AccountingRecordWithUserAndRoom accountingRecordWithUserAndRoom = accountingRecordService.selectByPrimaryKey(list.get(i).getRecordId());
            User user = userService.selectByPrimaryKey(list.get(i).getUserId());
            list.get(i).setAccountingRecord(accountingRecordWithUserAndRoom);
            list.get(i).setUser(user);
        }
        Long count = meetingMapper.selectByUserCount(criteria);
        pageBean.setResultList(list);
        pageBean.setTotalCount(count);
        return pageBean;
    }

    @Override
    public PageBean selectByRecord(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex()*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<MeetingWithRecordAndUser> list = meetingMapper.selectByRecord(criteria);
        for (int i=0; i<list.size(); i++){
            AccountingRecord accountingRecord = accountingRecordService.selectByPrimaryKey(list.get(i).getRecordId());
            User user = userService.selectByPrimaryKey(list.get(i).getUserId());
            list.get(i).setAccountingRecord(accountingRecord);
            list.get(i).setUser(user);
        }
        Long count = meetingMapper.selectByRecordCount(criteria);
        pageBean.setResultList(list);
        pageBean.setTotalCount(count);
        return pageBean;
    }

    @Override
    public int deleteByRecordId(List<Long> list) {
        return meetingMapper.deleteByRecordId(list);
    }

    @Override
    public int deleteByUserIdAndRecordId(Criteria criteria) {
        return meetingMapper.deleteByUserIdAndRecordId(criteria);
    }

    @Override
    public int insert(List<Meeting> list) {
        return meetingMapper.insert(list);
    }

    @Override
    public List selectByRecordAllUser(Criteria criteria) {
        List<MeetingWithRecordAndUser> list = meetingMapper.selectByRecordAllUser(criteria);
        for (int i=0; i<list.size(); i++){
            AccountingRecord accountingRecord = accountingRecordService.selectByPrimaryKey(list.get(i).getRecordId());
            User user = userService.selectByPrimaryKey(list.get(i).getUserId());
            list.get(i).setAccountingRecord(accountingRecord);
            list.get(i).setUser(user);
        }
        return list;
    }
}
